package com.stedu.mall.orderservice.controller;

import com.github.pagehelper.PageInfo;
import com.stedu.mall.common.annotation.Log;
import com.stedu.mall.common.bean.Order;
import com.stedu.mall.common.bean.OrderVo;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.MallException;
import com.stedu.mall.common.service.OrderService;
import com.stedu.mall.common.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    //添加
    @PostMapping
    public RespBean insert(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws MallException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId =(Integer) map.get("id");
        orderVo.setUserId(userId);

        orderService.insert(orderVo);
        return RespBean.ok("添加订单成功", orderVo);
    }
    //添加
    @Log(name = "账户支付")
    @PostMapping("/pay")
    public RespBean pay(@RequestBody OrderVo orderVo, @RequestHeader("token") String token) throws MallException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId =(Integer) map.get("id");
        orderVo.setUserId(userId);

        orderService.pay(orderVo);
        return RespBean.ok("支付成功~");
    }
    //根据id删除
    @DeleteMapping(("/{id}"))
    public RespBean delete(@PathVariable("id") Integer id) throws MallException {
        if (!orderService.delete(id)) {
            return RespBean.ok("删除失败");
        }

        return RespBean.ok("删除成功");
    }
    //根据id修改
    @PutMapping
    public RespBean update(@RequestBody Order order, @RequestHeader("token") String token) throws MallException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId =(Integer) map.get("id");
        if (!orderService.update(order,userId)) {
            return RespBean.ok("修改失败");
        }
        return RespBean.ok("修改成功");
    }
    //条件分页搜索
    @GetMapping
    public RespBean selectByPage (Order condition, Integer pageNum, Integer pageSize) throws MallException {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;

        PageInfo<Order> pageInfo = orderService.selectByCondition(condition, pageNum, pageSize);

        return RespBean.ok("",pageInfo);
    }

    //根据id查询
    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") String id, @RequestHeader("token") String token) throws MallException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId =(Integer) map.get("id");
        Order data = orderService.selectById(id, userId);
        return RespBean.ok("", data);
    }
    //查询当前用户所有的订单信息
    @GetMapping("/userAll")
    public RespBean selectByUser(Integer pageNum, Integer pageSize, @RequestHeader("token") String token) throws MallException {
        Map<String, Object> map = JwtUtils.parseJwtToMap(token);
        Integer userId =(Integer) map.get("id");
        Order condition = new Order();
        condition.setUserId(userId);
        PageInfo<Order> data = orderService.selectByCondition(condition, pageNum, pageSize);
        return RespBean.ok("", data);
    }
}
